//
//  CZCirculPlayPictureViewController.m
//  packDemo
//
//  Created by apple on 16/6/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "CZCirculPlayPictureViewController.h"
#import "CZCycle.h"
#import "CZCycleCell.h"
#import <Masonry.h>
#import <TAPageControl.h>
#import "CZShowPictureViewController.h"
@interface CZCirculPlayPictureViewController ()<TAPageControlDelegate>
/**
 * 轮播数组
 */
@property (nonatomic,strong) NSArray *cycles;
/**
 *  记录当前播放的图片
 */
@property (nonatomic,assign) NSInteger currentIndex;
/**
 *  记录第几组
 */
@property (nonatomic,assign) NSInteger currentSection;
/**
 *  pageConrol
 */
@property (nonatomic,strong) TAPageControl *pageControl;
@end

@implementation CZCirculPlayPictureViewController

static NSString * const reuseIdentifier = @"Cell";

-(instancetype)init{
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayOut.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 500);
    flowLayOut.minimumLineSpacing = 0;
    flowLayOut.minimumInteritemSpacing = 0;
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    return [super initWithCollectionViewLayout:flowLayOut];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载数据
    [self loadData];
    //设置初始化信息
    [self setup];
    //实现无线轮播
    [self cyclePlay];
    //加载pageControl
    [self setPageControl];
    
    //注册cell
    [self.collectionView registerClass:[CZCycleCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
//加载pageControl
-(void)setPageControl{
    self.pageControl= [[TAPageControl alloc]init];
    self.pageControl.delegate = self;
    self.pageControl.dotSize = CGSizeMake(10, 10);
    [self.view addSubview:self.pageControl];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@100);
        make.height.mas_equalTo(@30);
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view).offset(-70);
    }];

}
//设置初始化信息
- (void)setup{
    //0.去掉横向及竖向的滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    //去掉弹簧效果
    self.collectionView.bounces = NO;
    //设置分页效果
    self.collectionView.pagingEnabled = YES;
}

//加载轮播数据
-(void)loadData{
    __weak typeof(self) weakself = self;
    [CZCycle cycleWithCompleteBlock:^(NSArray *cycle) {
        weakself.cycles = cycle;
        [weakself.collectionView reloadData];
        
        self.pageControl.numberOfPages = cycle.count;

    }];

}
//实现轮播
-(void)cyclePlay{

    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(cycle) userInfo:nil repeats:YES];


}
//轮播播放
-(void)cycle{
    self.currentIndex++;

    if (self.currentIndex == 4) {
        self.currentIndex = 0;
        self.currentSection++;
    }
    NSIndexPath *index = [NSIndexPath indexPathForItem:self.currentIndex inSection:self.currentSection];
    self.pageControl.currentPage = self.currentIndex;
    [self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
}

#pragma mark <UICollectionViewDataSource>

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 500;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.cycles.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CZCycleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.cycle = self.cycles[indexPath.item];
    
    return cell;
}
//选择图片
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CZShowPictureViewController *showVc = [[CZShowPictureViewController alloc]init];
    [self.navigationController pushViewController:showVc animated:YES];
    showVc.iconPicture = self.cycles[indexPath.item];
    
}



- (void)TAPageControl:(TAPageControl *)pageControl didSelectPageAtIndex:(NSInteger)index{
    
    NSIndexPath *pageIndex = [NSIndexPath indexPathForItem:index inSection:self.currentSection];
    self.currentIndex = index;
    self.pageControl.currentPage = index;
    [self.collectionView scrollToItemAtIndexPath:pageIndex atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

}


@end
